﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class Admin_addjobcategory : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FillGrid();
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            JobSiteStarterKit.DAL.DBAccess obj = new JobSiteStarterKit.DAL.DBAccess();
            System.Data.SqlClient.SqlParameterCollection obj1 = (System.Data.SqlClient.SqlParameterCollection)obj.Parameters;
            obj1.Add("@CategoryName", SqlDbType.NVarChar, 200).Value = txtJobType.Text.Trim();
            obj1.Add("@CategoryRemarks", SqlDbType.NVarChar, 200).Value = txtremark.Text.Trim();
            obj.ExecuteNonQuery("Proc_InsertJobCategory");
            lblmsg.Visible = true;
            FillGrid();
            txtJobType.Text = "";
            txtremark.Text = "";

        }
        catch (Exception ex)
        {
            Response.Write(ex.StackTrace);
        }
    }
    protected void BtnRefresh_Click(object sender, EventArgs e)
    {
        txtJobType.Text = "";
        txtremark.Text = "";
        lblmsg.Visible =false;
        FillGrid();

    }
    private void FillGrid()
    {
          DataSet DsetJobType=new DataSet();
          JobSiteStarterKit.DAL.DBAccess obj = new JobSiteStarterKit.DAL.DBAccess();
          DsetJobType = obj.ExecuteDataSet("Proc_GetJobcategory");
          DGridcategory.DataSource = DsetJobType.Tables[0].DefaultView;
          DGridcategory.DataBind();
    }

    protected void DGridcategory_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string id = DGridcategory.DataKeys[e.RowIndex].Value.ToString();
        JobSiteStarterKit.DAL.DBAccess objDelete = new JobSiteStarterKit.DAL.DBAccess();
        System.Data.SqlClient.SqlParameterCollection objDelete1 = (System.Data.SqlClient.SqlParameterCollection)objDelete.Parameters;
        objDelete1.Add("@CategoryID", SqlDbType.NVarChar, 10).Value = id;
        objDelete.ExecuteNonQuery("Proc_DeleteJobCategory");
        FillGrid();
    }
    protected void DGridcategory_RowEditing(object sender, GridViewEditEventArgs e)
    {
        DGridcategory.EditIndex = e.NewEditIndex;
        FillGrid();
    }
    protected void DGridcategory_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        DGridcategory.EditIndex = -1;
        FillGrid();
    }
    protected void DGridcategory_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string category="";
        string remarks="";
        string id = DGridcategory.DataKeys[e.RowIndex].Value.ToString();
        JobSiteStarterKit.DAL.DBAccess objUpdate = new JobSiteStarterKit.DAL.DBAccess();
        System.Data.SqlClient.SqlParameterCollection objUpdate1 = (System.Data.SqlClient.SqlParameterCollection)objUpdate.Parameters;
        
        Control ctrcat = new Control();
        Control ctrremark = new Control();
        ctrcat = DGridcategory.Rows[e.RowIndex].FindControl("txtcat");
        ctrremark = DGridcategory.Rows[e.RowIndex].FindControl("txtRemark");
        if (ctrcat != null)
        {
            TextBox txtcategory = (TextBox)ctrcat;
            category = txtcategory.Text;
        }
        if (ctrremark != null)
        {
            TextBox txtRemark = (TextBox)ctrremark;
            remarks = txtRemark.Text;
        }
        objUpdate1.Add("@CategoryID", SqlDbType.NVarChar, 10).Value = id;
        objUpdate1.Add("@CategoryName", SqlDbType.NVarChar, 500).Value = category;
        objUpdate1.Add("@CategoryRemarks", SqlDbType.NVarChar,1000).Value =remarks;
        objUpdate.ExecuteNonQuery("Proc_UpdateJobCategory");
        DGridcategory.EditIndex = -1;
        FillGrid();
    }
    //protected void DGridcategory_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    //{
    //    string id = DGridcategory.DataKeys[e.RowIndex].Value.ToString();
    //    Response.Redirect("../Admin/AddSubCategory.aspx");

    //}
    protected void DGridcategory_SelectedIndexChanged(object sender, EventArgs e)
    {

        string id = Convert.ToString(DGridcategory.SelectedDataKey.Value);
        Response.Redirect("../Admin/AddSubCategory.aspx?jobcatid="+id);
    }
    protected void DGridcategory_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {

    }
}
